﻿Imports System.Data.OleDb
Imports DevComponents.DotNetBar

Public Class ucDSCongty

    Private dtCongty As DataTable
    Dim ctBus As New CompanyBUS()

    Private Sub newButton_Click(sender As Object, e As EventArgs) Handles newButton.Click
        Dim uc As New ucCapnhatcongty(0, grdCompany.SelectedRows(0))
        'Me.Controls.Add(uc)
        uc.Dock = DockStyle.Fill
        Dim parent = DirectCast(Application.OpenForms.Item(0), Main)
        parent.ShowModalPanel(uc, DevComponents.DotNetBar.Controls.eSlideSide.Left)
        'AddHandler uc.ButtonItemSave.MouseDown, AddressOf ButtonItemSave_MouseDown
        AddHandler uc.Closed, AddressOf uc_Closed
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        mdQuanLyBanHang.InitQuanLyBanHang()
        LoadData()
        ReloadWidthColumn()
        LoadComboboxSearch()
    End Sub

    Public Sub LoadData()
        dtCongty = ctBus.Company_List().Tables(0)
        grdCompany.DataSource = dtCongty
        grdCompany.Sort(grdCompany.Columns(3), System.ComponentModel.ListSortDirection.Descending)
    End Sub

    Public Sub showdatecontrols()
        tbSearchStr.Visible = False
        LabelXTukhoa.Visible = False
        LabelXTungay.Visible = True
        DateTimeInputTungay.Visible = True
        LabelXDenngay.Visible = True
        DateTimeInputDenngay.Visible = True
    End Sub

    Public Sub hidedatecontrols()
        tbSearchStr.Visible = True
        LabelXTukhoa.Visible = True
        LabelXTungay.Visible = False
        DateTimeInputTungay.Visible = False
        LabelXDenngay.Visible = False
        DateTimeInputDenngay.Visible = False
    End Sub

    Public Sub LoadComboboxSearch()
        For Each col As DataGridViewColumn In grdCompany.Columns
            If col.Visible = True Then
                Dim ci As New ComboBoxItem(col.Name, col.HeaderText)
                ci.Tag = col.Tag
                cbSearchIn.Items.Add(ci)
            End If
        Next

        cbSearchIn.SelectedIndex = 0
    End Sub

    Public Sub ReloadWidthColumn()

        grdCompany.Columns(0).Width = 200
        grdCompany.Columns(0).HeaderText = "Tên công ty"
        grdCompany.Columns(0).Name = "tencongty"
        grdCompany.Columns(0).Tag = "string"

        grdCompany.Columns(1).Width = 200
        grdCompany.Columns(1).Visible = False

        grdCompany.Columns(2).Width = 250
        grdCompany.Columns(2).HeaderText = "Địa chỉ"
        grdCompany.Columns(2).Name = "diachi"
        grdCompany.Columns(2).Tag = "string"

        grdCompany.Columns(3).Width = 110
        grdCompany.Columns(3).HeaderText = "Mã số thuế"
        grdCompany.Columns(3).Name = "tax"
        grdCompany.Columns(3).Tag = "string"

        grdCompany.Columns(4).Width = 150
        grdCompany.Columns(4).HeaderText = "Số điện thoại bàn 1"
        grdCompany.Columns(4).Name = "sdt1"
        grdCompany.Columns(4).Tag = "string"

        grdCompany.Columns(5).Width = 150
        grdCompany.Columns(5).HeaderText = "Số điện thoại bàn 2"
        grdCompany.Columns(5).Name = "sdt2"
        grdCompany.Columns(5).Tag = "string"

        grdCompany.Columns(6).Width = 150
        grdCompany.Columns(6).HeaderText = "Số điện thoại di động"
        grdCompany.Columns(6).Name = "sdtdt"
        grdCompany.Columns(6).Tag = "string"

        grdCompany.Columns(7).Width = 150
        grdCompany.Columns(7).HeaderText = "Số FAX"
        grdCompany.Columns(7).Name = "fax"
        grdCompany.Columns(7).Tag = "string"

        grdCompany.Columns(8).Width = 150
        grdCompany.Columns(8).HeaderText = "Ghi chú"
        grdCompany.Columns(8).Name = "ghichu"
        grdCompany.Columns(8).Tag = "string"

        grdCompany.Columns(9).Width = 150
        grdCompany.Columns(9).HeaderText = "Email"
        grdCompany.Columns(9).Name = "email"
        grdCompany.Columns(9).Tag = "string"

        grdCompany.Columns(10).Width = 150
        grdCompany.Columns(10).HeaderText = "Website"
        grdCompany.Columns(10).Name = "website"
        grdCompany.Columns(10).Tag = "string"

        grdCompany.Columns(11).Visible = False

        grdCompany.Columns(12).Width = 150
        grdCompany.Columns(12).HeaderText = "Người tạo"
        grdCompany.Columns(12).Name = "nguoitao"
        grdCompany.Columns(12).Tag = "string"

        grdCompany.Columns(13).Width = 150
        grdCompany.Columns(13).HeaderText = "Ngày tạo"
        grdCompany.Columns(13).Name = "ngaytao"
        grdCompany.Columns(13).Tag = "date"

        grdCompany.Columns(14).Width = 150
        grdCompany.Columns(14).HeaderText = "Người sữa"
        grdCompany.Columns(14).Name = "nguoisua"
        grdCompany.Columns(14).Tag = "string"

        grdCompany.Columns(15).Width = 150
        grdCompany.Columns(15).HeaderText = "Ngày sữa"
        grdCompany.Columns(15).Name = "ngaysua"
        grdCompany.Columns(15).Tag = "date"




    End Sub

    Private Function validation() As Boolean
        Try

            Select Case cbSearchIn.SelectedIndex
                Case 2, 4
                    If DateTimeInputTungay.Value > DateTimeInputDenngay.Value Then
                        BalloonTip1.SetBalloonCaption(DateTimeInputDenngay, "Lỗi tìm kiếm")
                        BalloonTip1.SetBalloonText(DateTimeInputDenngay, "Ngày bắt đầu phải trước ngày kết thúc")
                        BalloonTip1.ShowBalloon(tbSearchStr)
                    End If

                Case Else
                    If tbSearchStr.Text = "" Then
                        BalloonTip1.SetBalloonCaption(tbSearchStr, "Lỗi tìm kiếm")
                        BalloonTip1.SetBalloonText(tbSearchStr, "Vui lòng nhập thông tin")
                        BalloonTip1.ShowBalloon(tbSearchStr)
                        Return False
                    End If
            End Select
            Return True

        Catch ex As Exception

        End Try

        Return True
    End Function

    Private Sub cbSearchIn_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbSearchIn.SelectedIndexChanged
        If cbSearchIn.SelectedIndex = 11 Or cbSearchIn.SelectedIndex = 13 Then
            showdatecontrols()
            DateTimeInputDenngay.Value = Now
            DateTimeInputTungay.Value = Now
        Else
            hidedatecontrols()
        End If
    End Sub

    Private Sub btEdit_Click(sender As Object, e As EventArgs) Handles btEdit.Click
        Dim uc As New ucCapnhatcongty(1, grdCompany.SelectedRows(0))
        uc.Dock = DockStyle.Fill
        Dim parent = DirectCast(Application.OpenForms.Item(0), MetroAppForm)
        parent.ShowModalPanel(uc, DevComponents.DotNetBar.Controls.eSlideSide.Left)
        AddHandler uc.Closed, AddressOf uc_Closed

        'If uc.ButtonItemSave.IsMouseDown Then
        '    LoadData()
        'End If
    End Sub

    Private Sub uc_Closed()
        LoadData()
    End Sub



    Private Sub FindNow_Click(sender As Object, e As EventArgs) Handles FindNow.Click

    End Sub


    Private Sub deleteButton_Click(sender As Object, e As EventArgs) Handles deleteButton.Click
        Try
            BalloonTip1.SetBalloonCaption(grdCompany, "Xóa Công ty ")
            If MessageBox.Show("Bạn có chắc chắn muốn xóa Công ty " + grdCompany.SelectedRows(0).Cells(0).Value.ToString(), "Thông báo", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                If ctBus.Company_Delete(grdCompany.SelectedRows(0).Cells(1).Value) Then
                    BalloonTip1.SetBalloonText(grdCompany, "Thành công")
                    BalloonTip1.ShowBalloon(grdCompany)
                    LoadData()
                Else
                    BalloonTip1.SetBalloonText(grdCompany, "Thất bại ! Công ty " + grdCompany.SelectedRows(0).Cells(0).Value.ToString() + " vẫn còn được sữ dụng ")
                    BalloonTip1.ShowBalloon(grdCompany)
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btReport_Click(sender As Object, e As EventArgs) Handles btReport.Click
        MessageBox.Show("Đang xử lý hiển thị danh sách công ty này, vui lòng chờ", "Thông báo", MessageBoxButtons.OK)
        PrintReport(False)
    End Sub

    Private Sub PrintReport(ByVal bolPrint As Boolean)
        Dim frmPrint As New frmReportViewer
        Dim strSelectionFormula As String = Nothing
        Dim rowGrid As DataGridViewRow
        Try
            For Each rowGrid In grdCompany.Rows
                If Not IsNothing(strSelectionFormula) Then
                    strSelectionFormula += " OR "
                End If
                strSelectionFormula += " {TDT_Company.CompanyID} = " & rowGrid.Cells("CompanyID").Value
            Next
            frmPrint.Text = Me.Text
            frmPrint.SelectionFormula = strSelectionFormula
            If bolPrint = False Then
                frmPrint.InitializeLifetimeService()
                frmPrint.ConfigReport("crpCompany.rpt", False)
                frmPrint.Show()
            Else
                frmPrint.ConfigReport("crpCompany.rpt", True)
                frmPrint.PrintReport()
            End If
        Catch ex As Exception
            objErrorLog.SaveError(ex.Message, Me.Name, "PrintReport", objUser.UserFullName)
            'SendError(ex)
        End Try
    End Sub

    Private Sub btPrint_Click(sender As Object, e As EventArgs) Handles btPrint.Click
        If MessageBox.Show("Bạn sẽ in danh sách công ty này", "Thông báo", MessageBoxButtons.YesNo) = DialogResult.Yes Then
            PrintReport(True)
        End If
    End Sub
End Class
